/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BillManagement.java
 *
 * Created on Mar 27, 2011, 11:05:43 PM
 */
package vn.aptech.project.view;

import vn.aptech.project.control.Customer_BLL;
import vn.aptech.project.control.category_BLL;
import vn.aptech.project.control.orderline_BLL;
import vn.aptech.project.control.tblorder_BLL;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mr.Hiep
 */
public class BillManagementStaff extends javax.swing.JPanel {

    /** Creates new form BillManagement */
    public BillManagementStaff() throws IOException, SQLException {
        initComponents();
        SelectOrder();
    }

    public void SelectOrder() throws IOException {

        try {
            tblorder_BLL tob = new tblorder_BLL();
            Vector vt;
            if (txtCustomername.getText().equals("")) {
                txtCustomername.setText("Enter customer name");
            }
            if (txtOrderid.getText().equals("")) {
                txtOrderid.setText("Enter order id");
            }
            if (!txtCustomername.getText().equals("Enter customer name") && !txtOrderid.getText().equals("Enter order id")) {
                vt = tob.SearchByOrderIdAndCustName1(Integer.parseInt(txtOrderid.getText()), txtCustomername.getText());

            } else if (!txtCustomername.getText().equals("Enter customer name")) {
                vt = tob.SearchByCustomerName1(txtCustomername.getText());

            } else if (!txtOrderid.getText().equals("Enter order id")) {
                vt = tob.SearchByOrderId1(Integer.parseInt(txtOrderid.getText()));

            } else {
                vt = tob.selecttblorder1();
            }
            Vector vtCol = new Vector();
            vtCol.add("Id");
            vtCol.add("Customer name");
            vtCol.add("Contact_no");
            vtCol.add("Address");
            vtCol.add("Purchase date");
            vtCol.add("Total Amount");
            vtCol.add("Booth");
            vtCol.add("Seller");
            DefaultTableModel dtm = new DefaultTableModel(vt, vtCol) {

                @Override
                public boolean isCellEditable(int rowIndex, int mColIndex) {
                    return false;
                }
            };

            tblOrder.setModel(dtm);
               for (int i = 0; i <= tblOrder.getRowCount() - 1; i++) {
                int cateid = Integer.parseInt(tblOrder.getValueAt(i, 6).toString());
                category_BLL ctb = new category_BLL();
                String catename = ctb.getCategoryNameByCategoryId(cateid);
                tblOrder.setValueAt(catename, i, 6);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblOrder = new javax.swing.JTable();
        txtCustomername = new javax.swing.JTextField();
        txtOrderid = new javax.swing.JTextField();
        bntSearchOrder = new javax.swing.JButton();
        bntOrderDetails = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        tblOrder.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblOrder);

        txtCustomername.setText("Enter customer name");

        txtOrderid.setText("Enter order id");
        txtOrderid.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtOrderidKeyTyped(evt);
            }
        });

        bntSearchOrder.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Search.png"))); // NOI18N
        bntSearchOrder.setText("Search");
        bntSearchOrder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntSearchOrderActionPerformed(evt);
            }
        });

        bntOrderDetails.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/posts.gif"))); // NOI18N
        bntOrderDetails.setText("Order Details");
        bntOrderDetails.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntOrderDetailsActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add.png"))); // NOI18N
        jButton1.setText("Create Order");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/edit.png"))); // NOI18N
        jButton2.setText("Edit Order");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/delete.png"))); // NOI18N
        jButton3.setText("Delete Order");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(bntOrderDetails)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(txtCustomername, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(txtOrderid, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(bntSearchOrder, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3)))
                .addGap(25, 25, 25))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCustomername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bntSearchOrder)
                    .addComponent(txtOrderid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bntOrderDetails)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap(29, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void bntSearchOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntSearchOrderActionPerformed
        try {
            SelectOrder();
        } catch (IOException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bntSearchOrderActionPerformed

    private void bntOrderDetailsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntOrderDetailsActionPerformed
        int currentline = tblOrder.getSelectedRow();

        if (currentline < 0) {
            JOptionPane.showMessageDialog(this, "Select a line");
        } else {
            int abc = Integer.parseInt(String.valueOf(tblOrder.getValueAt(currentline, 0)));
            variable.orderid = abc;
            OrderDetails od = new OrderDetails(null, true);
            od.show();
        }
    }//GEN-LAST:event_bntOrderDetailsActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            CreateOrder co = new CreateOrder(null, true);
            co.setVisible(true);
            SelectOrder();
        } catch (IOException ex) {
            Logger.getLogger(Staff.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        int currentline = tblOrder.getSelectedRow();

        if (currentline < 0) {
            JOptionPane.showMessageDialog(this, "Select a line");
        } else {
            variable.CustomerName = tblOrder.getValueAt(currentline, 1).toString();
            variable.CustomerContact = Integer.parseInt(tblOrder.getValueAt(currentline, 2).toString());
            variable.CustomerAddress = tblOrder.getValueAt(currentline, 3).toString();
            variable.orderid = Integer.parseInt(tblOrder.getValueAt(currentline, 0).toString());
            variable.TotalAmount = Float.parseFloat(tblOrder.getValueAt(currentline, 5).toString());
            EditOrder eo;
            try {
                eo = new EditOrder(null, true);
                eo.setVisible(true);
            } catch (SQLException ex) {
                Logger.getLogger(Staff.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Staff.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Staff.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                CreateOrder co = new CreateOrder(null, true);
                SelectOrder();
            } catch (IOException ex) {
                Logger.getLogger(Staff.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        int reply = JOptionPane.showConfirmDialog(this, "Sure you delete", null, JOptionPane.OK_CANCEL_OPTION);
        if (reply == JOptionPane.YES_OPTION) {
            try {
                int currentline = tblOrder.getSelectedRow();
                if (currentline < 0) {
                    JOptionPane.showMessageDialog(this, "Select a line");
                } else {
                    int a = Integer.parseInt(String.valueOf(tblOrder.getValueAt(currentline, 0)));
                    tblorder_BLL order = new tblorder_BLL();
                    orderline_BLL odlb = new orderline_BLL();
                    Customer_BLL ctmb = new Customer_BLL();
                    int b = order.getCustomerIdByOrderid(a);
                    odlb.deleteorderline(a);
                    order.deletettblorder(a);
                    ctmb.deleteCustomer(b);
                    SelectOrder();
                    JOptionPane.showMessageDialog(this, "Sucessful Delete");
                }
            } catch (IOException ex) {
                Logger.getLogger(Staff.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void txtOrderidKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtOrderidKeyTyped
    int key = evt.getKeyChar();
        String st = txtOrderid.getText();
        String stTest = "0123456789";
        if (key != KeyEvent.VK_BACK_SPACE
                && key != KeyEvent.VK_DELETE
                && key != KeyEvent.VK_ENTER) {
            int flag = 0;
            if (stTest.indexOf(evt.getKeyChar()) == -1) {
                flag++;
            }
            if (flag > 0) {
                evt.consume();
            }
        }        // TODO add your handling code here:
    }//GEN-LAST:event_txtOrderidKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bntOrderDetails;
    private javax.swing.JButton bntSearchOrder;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblOrder;
    private javax.swing.JTextField txtCustomername;
    private javax.swing.JTextField txtOrderid;
    // End of variables declaration//GEN-END:variables
}
